import pandas as pd
import numpy as np

# DataFrame的多层索引操作
# 1.索引
data= np.random.randint(10,100,size=(6,6))
index = [
    ['1班','1班','1班','2班','2班','2班'],
    ['小白','小黑','小红','小紫','小黄','小绿']
]
columns = [
    ['期中','期中','期中','期末','期末','期末'],
    ['语文','数学','英语','物理','化学','生物']
]
df = pd.DataFrame(data,index=index,columns=columns)
print(df)

# 列切片
print("\n列切片")
print(df.iloc[:,1:3])
print(df.loc[:,'期中':'期末'])


# 行切片
print("\n行切片")
print(df.iloc[1:5])
print(df.loc['1班':'2班'])
print(df.loc['1班':'2班','小白':'小绿'])


# 行索引
print("\n行索引")
print(df.loc['1班'])
print(df.loc['1班'].loc['小白'])
print(df.loc['1班', '小白'])
print(df.loc[('1班','小白')])

print(df.iloc[1])
print(df.iloc[[1]])
print(df.iloc[[1,2,3]])

# 列索引
print("\n列索引")
print(df['期中'])
print(df['期中']['语文'])
print(df['期中'][['语文']])
print(df['期中', '语文'])
print(df.期中.数学)








# 获取元素
print("\n获取元素")
print(df['期中']['语文']['1班']['小白'])
print(df['期中']['语文']['1班'][0])
print(df.iloc[0,0])
print(df.loc[('1班','小白'),('期中','语文')])





